using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Linq;
//using System.Xml.Linq;


namespace CommonUtils
{
	public class ConvertUtils
	{
		public static string[] Matrix2Vector(string[,] matrix)
		{
			// Temp:
			if (matrix == null) return null;
			int m = matrix.GetLength(0);
			int n = matrix.GetLength(1);
			string[] vector = new string[m * n];

			// Scan:
			for (int i = 0; i < m; i++)
				for (int j = 0; j < n; j++)
					vector[i * n + j] = matrix[i, j];

			// Return Good:
			return vector;
		}
		public static string[,] Vector2Matrix(string[] vector, int nColumnCnt)
		{
			// Temp:
			if (vector == null) return null;
			int m = vector.Length / nColumnCnt; // The truncate will occur if surplus.
			int n = nColumnCnt;
			string[,] matrix = new string[m, n];

			// Scan:
			for (int i = 0; i < m; i++)
				for (int j = 0; j < n; j++)
					matrix[i, j] = vector[i * n + j];

			// Return Good:
			return matrix;
		}
		public static object[] Matrix2Vector(object[,] matrix)
		{
			// Temp:
			if (matrix == null) return null;
			int m = matrix.GetLength(0);
			int n = matrix.GetLength(1);
			object[] vector = new object[m * n];

			// Scan:
			for (int i = 0; i < m; i++)
				for (int j = 0; j < n; j++)
					vector[i * n + j] = matrix[i, j];

			// Return Good:
			return vector;
		}
		public static object[,] Vector2Matrix(object[] vector, int nColumnCnt)
		{
			// Temp:
			if (vector == null) return null;
			int m = vector.Length / nColumnCnt; // The truncate will occur if surplus.
			int n = nColumnCnt;
			object[,] matrix = new object[m, n];

			// Scan:
			for (int i = 0; i < m; i++)
				for (int j = 0; j < n; j++)
					matrix[i, j] = vector[i * n + j];

			// Return Good:
			return matrix;
		}

		public static bool ArrayByte2File(string strFileName, byte[] arrbyteData)
		{
			// Process:
			System.IO.FileStream fs = new System.IO.FileStream(strFileName, FileMode.Create, FileAccess.Write);
			fs.Write(arrbyteData, 0, arrbyteData.Length);
			fs.Close();

			// Return Good:
			return true;
		}

		public static byte[] Resource2ArrayBytes(Assembly asm, string strResourceBaseName, string strResourceID)
		{
			ResourceManager resman = new ResourceManager(strResourceBaseName, asm);
			byte[] arrbyteTemp = (byte[])resman.GetObject(strResourceID);

			// Return Good:
			return arrbyteTemp;
		}
		public static byte[] Resource2ArrayBytes(string strResourceBaseName, string strResourceID)
		{
			return Resource2ArrayBytes(Assembly.GetExecutingAssembly(), Assembly.GetExecutingAssembly().GetName().Name + "." + strResourceBaseName, strResourceID);
		}

		public static string[] ToUpper(string[] arrstr)
		{
			// Temp:
			if (arrstr == null) return null;
			string[] arrstrRes = new string[arrstr.Length];
			for (int i = 0; i < arrstr.Length; i++)
				arrstrRes[i] = (arrstr[i] == null ? null : arrstr[i].ToUpper());

			// Return Good:
			return arrstrRes;
		}
		public static string[,] ToUpper(string[,] matrixstr)
		{
			// Temp:
			if (matrixstr == null) return null;
			int m = matrixstr.GetLength(0);
			int n = matrixstr.GetLength(1);
			string[,] matrixstrRes = new string[m, n];
			for (int i = 0; i < m; i++)
				for (int j = 0; j < n; j++)
					matrixstrRes[i, j] = (matrixstr[i, j] == null ? null : matrixstr[i, j].ToUpper());

			// Return Good:
			return matrixstrRes;
		}
		public static string[] ToLower(string[] arrstr)
		{
			// Temp:
			if (arrstr == null) return null;
			string[] arrstrRes = new string[arrstr.Length];
			for (int i = 0; i < arrstr.Length; i++)
				arrstrRes[i] = (arrstr[i] == null ? null : arrstr[i].ToLower());

			// Return Good:
			return arrstrRes;
		}
		public static string[,] ToLower(string[,] matrixstr)
		{
			// Temp:
			if (matrixstr == null) return null;
			int m = matrixstr.GetLength(0);
			int n = matrixstr.GetLength(1);
			string[,] matrixstrRes = new string[m, n];
			for (int i = 0; i < m; i++)
				for (int j = 0; j < n; j++)
					matrixstrRes[i, j] = (matrixstr[i, j] == null ? null : matrixstr[i, j].ToLower());

			// Return Good:
			return matrixstrRes;
		}

		public static string[] CopyArray(string[] arrstr)
		{
			// Temp:
			if (arrstr == null) return null;
			int m = arrstr.Length;
			string[] arrstrTemp = new string[m];

			// Scan:
			for (int i = 0; i < m; i++)
				arrstrTemp[i] = arrstr[i];

			// Return Good:
			return arrstrTemp;
		}
		public static string[,] CopyMatrix(string[,] matrixstr)
		{
			// Temp:
			if (matrixstr == null) return null;
			int m = matrixstr.GetLength(0);
			int n = matrixstr.GetLength(1);
			string[,] matrixstrTemp = new string[m, n];

			// Scan:
			for (int i = 0; i < m; i++)
				for (int j = 0; j < n; j++)
					matrixstrTemp[i, j] = matrixstr[i, j];

			// Return Good:
			return matrixstrTemp;
		}
	}
}
